package com.ruoyi.base.page;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-05-30 08:33:15
 */
@Data
@ApiModel
public class PageSimple<T> {

    /**
     * 页码，从1开始
     */
    @ApiModelProperty(value = "页码")
    private int pageNum;
    /**
     * 页面大小
     */
    @ApiModelProperty(value = "每页条目数")
    private int pageSize;
    /**
     * 当前页的数量
     */
    @ApiModelProperty(value = "当前页的数量")
    private int size;
    /**
     * 总数
     */
    @ApiModelProperty(value = "总数")
    private long total;
    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数")
    private int pages;
    /**
     * 数据列表
     */
    @ApiModelProperty(value = "数据列表")
    private List<T> list;

    /**
     * 分页结果
     */
    public PageSimple(List<T> list) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.size = page.size();
        } else {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.total = list.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.size = list.size();
        }

        this.list = list;
    }

    /**
     * 分页结果
     */
    public PageSimple(List<T> list, long total, int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.pages = (int) total / pageSize + (total % pageSize > 0 ? 1 : 0);
        this.size = list.size();
        this.list = list;
    }

    /**
     * 空结果
     */
    public PageSimple() {
        this.list = Collections.emptyList();
        this.pageNum = 1;
        this.pageSize = 20;
        this.total = 0;
        this.pages = 0;
        this.size = 0;
    }
}
